package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Faillog;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.mapper.FaillogMapper;
import cn.wolfcode.crm.query.impl.FaillogQuery;
import cn.wolfcode.crm.service.IFaillogService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class FaillogServiceImpl implements IFaillogService {
    @Autowired
    FaillogMapper faillogMapper;
    @Autowired
    CustomerMapper customerMapperpper;

    public void insert(Long id, String text) {
        Customer customer = customerMapperpper.selectByPrimaryKey(id);
        Faillog faillog = new Faillog();
        faillog.setCus(customer);
        if (customer.getInchargeuser() != null) {
            faillog.setCharge(customer.getInchargeuser());
        }
        faillog.setDate(new Date());
        faillog.setState(false);
        faillog.setText(text);
        faillogMapper.insert(faillog);

    }

    public void saverun(Long id, String text) {
        Customer customer = customerMapperpper.selectByPrimaryKey(id);
        Faillog faillog = new Faillog();
        faillog.setCus(customer);
        if (customer.getInchargeuser() != null) {
            faillog.setCharge(customer.getInchargeuser());
        }
        faillog.setDate(new Date());
        faillog.setState(true);
        faillog.setText(text);
        faillogMapper.insert(faillog);

    }

    public Faillog getOne(Long id) {
        return faillogMapper.selectByPrimaryKey(id);
    }

    public List<Faillog> selectAll() {
        return faillogMapper.selectAll();
    }

    public void update(Faillog record) {
        faillogMapper.updateByPrimaryKey(record);
    }

    public PageResult query(FaillogQuery query) {
        int rows = faillogMapper.queryForCount(query);
        if (rows == 0) {
            new PageResult(rows, Collections.EMPTY_LIST);
        }
        List<Faillog> result = faillogMapper.queryForList(query);
        return new PageResult(rows, result);
    }
}
